// stdafx.cpp : source file that includes just the standard includes
// Ooganizer-Hook.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"

//////////////////////////////////////////////////////////////////////
// Func: SetLoggingDetail()
//////////////////////////////////////////////////////////////////////
void SetLoggingDetail(LoggingDetail detail, char* logPath)
{
	g_LoggingDetail = detail;
	
	// If the log path is set externally set it here internally
	// else set the log path to %USERPROFILE%\AppData\Local\Temp
	if(logPath && logPath[0] != 0)
	{
		strncpy(g_LogPath, logPath, MAX_PATH);
	}
	else
	{
		size_t len = 0;
		errno_t err = 0;
		char* pValue = NULL;

		if(err = _dupenv_s( &pValue, &len, "TMP"))	
			if(err = _dupenv_s( &pValue, &len, "TEMP"))
				return;		
		
		if(pValue != NULL)
			strcpy_s(g_LogPath, pValue);
	}

	// Make sure Path ends with '\'
	if (g_LogPath[strlen(g_LogPath) - 1] != '\\')
				strcat_s(g_LogPath,"\\");

	// Add the Log File name to the path variable (now we have everything)
	// ~we are ready to do some serious logging
	strcat_s(g_LogPath,g_LOG_FILE_NAME);	
}
//////////////////////////////////////////////////////////////////////
// Func: GetLoggingDetail()
//////////////////////////////////////////////////////////////////////
_LoggingDetail GetLoggingDetail()
{
	return g_LoggingDetail;
}
//////////////////////////////////////////////////////////////////////
// Func: log()
// Desc: Logs to Ooganizer.log in defined LogPath or temporary folder
// Prms: Variable # of params
//
// Usge: Log("%s %d %d %d", "test", 1, 2, 3)
//////////////////////////////////////////////////////////////////////
void log( LoggingDetail detail, char * pFmt, ... )
{	
	if(g_LoggingDetail >= detail)
	{								
		FILE* pFile = NULL;	
		errno_t err = 0;

		if ( !( err = fopen_s( &pFile, g_LogPath, "at" ) ) )
		{

			fprintf( pFile, "ThreadId %i -", GetCurrentThreadId());
			va_list	arg;
			va_start( arg, pFmt );
			vfprintf( pFile, pFmt, arg );
			va_end( arg );

			if ( pFmt[strlen( pFmt ) - 1] != '\n' )
				fprintf( pFile, "\n" );
			fclose( pFile );
		}		
	}
}
//////////////////////////////////////////////////////////////////////
// Func: clearLog()
// Desc: clears the log file (used at startup) - Only Clears if Logging is enabled
//////////////////////////////////////////////////////////////////////
void clearLog()
{
	if(g_LoggingDetail != LOGGING_NONE)
	{		
		FILE* pFile = NULL;	
		errno_t err = 0;
		
		if ( !( err = fopen_s( &pFile, g_LogPath, "w" ) ) )
			fclose( pFile );					
	}	
}
//////////////////////////////////////////////////////////////////////
// Func: log()
// Desc: Logs the window title text to the Log File
// Prms: HWND, handle to a window
//		 str, custom string to log
//
// Note: GetWindowTextA doesn't always return TRUE, if this function
//		 gets called during the WndProc it will return False
//////////////////////////////////////////////////////////////////////
void log(LoggingDetail detail, HWND hWnd, char* str)
{
	if(g_LoggingDetail >= detail)
	{
		char Title[MAX_PATH + 1] = {0};
		
		if (GetWindowTextA(hWnd,Title,MAX_PATH))
		{
			log(detail,"Window - %s - %s", Title, str);
		}
	}	
}